package com.nulstudio.cloud.account.controller;

import com.nulstudio.cloud.account.entity.NulInvite;
import com.nulstudio.cloud.account.request.GenerateTokenRequest;
import com.nulstudio.cloud.account.service.InviteService;
import com.nulstudio.cloud.common.util.NulResult;
import jakarta.annotation.Resource;
import org.springframework.lang.NonNull;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/invite")
public class InviteController {
    @Resource
    @NonNull
    private InviteService inviteService;

    @PreAuthorize("hasAuthority('admin:write')")
    @PostMapping
    @NonNull
    public NulResult<String> generate(@RequestBody @NonNull GenerateTokenRequest request) {
        final NulInvite invite = inviteService.generateInvite(request.expireTime(), request.role(),
                request.remaining(), request.blocked());
        return NulResult.response(invite.getCode());
    }
}
